import tkinter as tk
from tkinter import messagebox

class application(tk.Frame):
    def __init__(self,master=None):
        super().__init__(master)
        self.master=master
        self.creatWidget()

    def creatWidget(self):
        d={
            'width':8,
            'height':2
        }
        self.v1=tk.StringVar()
        self.e1=tk.Entry(self.master,textvariable=self.v1,justify="right",)
        self.e1.grid(row=0,column=0,columnspan=4,stick='ew')

        '''
            mc m+ m- mr
            (  )  %  C
            7  8  9  /
            4  5  6  x
            1  2  3  -
            0  .  =  +
        '''
        tmp_ls=['mc','m+','m-','mr','(',")",'%','c','7','8','9','/','4','5','6','x','1','2','3','-','0','.','=','+']
        self.calc={}
        row,column=1,-1
        for index,val in enumerate(tmp_ls):
            column+=1
            tmp=tk.Button(self.master,text=val,cnf=d)
            tmp.grid(row=row,column=column,stick='ew')
            self.calc[val]=tmp
            if (index+1)%4==0:
                row+=1
                column=-1

        self.calc['=']['command']=self.pt
        # self.ls[5][2]['command']=self.pt


    def pt(self):
        info=self.e1.get()
        try:
            if '.' in info:
                info=float(eval(info))
            else:
                info=int(eval(info))
        except:
            messagebox.showinfo('erro','请正确输入')
        self.v1.set(str(info))

window=tk.Tk()
window.title("计算器")

app=application(window)
window.mainloop()